﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class Mem_Rebate
    {
        public int Add(Chain.Model.Mem_Rebate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Mem_Rebate(");
            builder.Append("MemID,StartTime,EndTime,isonline,Level1,Level2,Level3,isdel)");
            builder.Append(" values (");
            builder.Append("@MemID,@StartTime,@EndTime,@isonline,@Level1,@Level2,@Level3,0)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] {
                new SqlParameter("@MemID", SqlDbType.Int,4),
                new SqlParameter("@StartTime", SqlDbType.DateTime,4),
                new SqlParameter("@EndTime", SqlDbType.DateTime,4),
                new SqlParameter("@isonline", SqlDbType.Int,4),
                new SqlParameter("@Level1", SqlDbType.Decimal,4),
                new SqlParameter("@Level2", SqlDbType.Decimal,4),
                new SqlParameter("@Level3", SqlDbType.Decimal,4)
             };
            cmdParms[0].Value = model.MemID;
            cmdParms[1].Value = model.StartTime;
            cmdParms[2].Value = model.EndTime;
            cmdParms[3].Value = model.isonline;
            cmdParms[4].Value = model.Level1;
            cmdParms[5].Value = model.Level2;
            cmdParms[6].Value = model.Level3;


            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }
        public Chain.Model.Mem_Rebate GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 *");
            builder.Append(" from Mem_Rebate");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }
        public Chain.Model.Mem_Rebate GetModel(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 *");
            builder.Append(" from Mem_Rebate");
            builder.Append(" where isdel=0 ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            
            DataSet set = DbHelperSQL.Query(builder.ToString());
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }
        public int GetModelLevel(Chain.Model.Mem_Rebate model)
        {
            int level = 0;
            if (model==null)
            {
                return 0;
            }
            if (model.Level1!=null&&model.Level1>0)
            {
                level = 1;
                if (model.Level2!=null&&model.Level2>0)
                {
                    level = 2;
                    if (model.Level3!=null&&model.Level3>0)
                    {
                        level = 3;
                    }
                }
            }
            return level;
        }
        public Chain.Model.Mem_Rebate DataRowToModel(DataRow row)
        {
            Chain.Model.Mem_Rebate model = new Chain.Model.Mem_Rebate();
            if (row != null)
            {
                if ((row["ID"] != null) && (row["ID"].ToString() != ""))
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if ((row["MemID"] != null) && (row["MemID"].ToString() != ""))
                {
                    model.MemID = int.Parse(row["MemID"].ToString());
                }
                if ((row["StartTime"] != null) && (row["StartTime"].ToString() != ""))
                {
                    model.StartTime = DateTime.Parse(row["StartTime"].ToString());
                }
                if ((row["EndTime"] != null) && (row["EndTime"].ToString() != ""))
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if ((row["isonline"] != null) && (row["isonline"].ToString() != ""))
                {
                    model.isonline = int.Parse(row["isonline"].ToString());
                }
                if ((row["Level1"] != null) && (row["Level1"].ToString() != ""))
                {
                    model.Level1 = decimal.Parse(row["Level1"].ToString());
                }
                if ((row["Level2"] != null) && (row["Level2"].ToString() != ""))
                {
                    model.Level2 = decimal.Parse(row["Level2"].ToString());
                }
                if ((row["Level3"] != null) && (row["Level3"].ToString() != ""))
                {
                    model.Level3 = decimal.Parse(row["Level3"].ToString());
                }
                if (row["isdel"] != null && (row["isdel"].ToString() != ""))
                {
                    model.isdel = int.Parse(row["isdel"].ToString());
                }
            }
            return model;
        }
        public bool UpdateClose(int MemID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Mem_Rebate set ");
            builder.Append("isonline=0");
            builder.Append(" where MemID=@MemID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;

            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public bool IsExist(int MemID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Mem_Rebate");
            builder.Append(" where MemID=@MemID and isonline=1");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

    }
}
